<?php
/**
 * Created by PhpStorm.
 * User: duanshuai
 * Date: 2018/3/24
 * Time: 上午11:25
 */

namespace app\index\model;

use think\Model;
class DailyBill extends Model
{

    //关联type_bill
    public function typebill()
    {
        return $this->belongsTo('TypeBill','type_id','id');
    }


    //增加账单
    public function addBill($param){

//        $model = new DailyBill();
        self::save(array_filter($param));




    }
    //查询个人账单
    /*  asc   desc
     * 按照时间顺序
     * */

    public function findCountBill($para){
        $count = $para['count'];
        $page = $para['page'];
        $list = self::where('count',$count)-> order('create_time','desc') -> limit(10*($page-1),10*$page) ->field('cost,create_time,type,comment')-> select();

        ;
        if ($list) {
            $list = collection($list)->toArray();
        }

        return $list;
    }

    //查询个人账单 不分页
    /*  asc   desc
     * 按照时间顺序
     * */

    public function findCountBill_all($count){
        $list = self::where('count',$count)-> order('create_time','desc') -> field('cost,create_time,type,comment')-> select();
        if ($list) {
            $list = collection($list)->toArray();
        }

        return $list;
    }

    /*
     * 分月的详细数据
     * */
    public function findCountBill_group_month($count){
        $months = self::query('select create_time from ds_daily_bill where count=? group by month(FROM_UNIXTIME(create_time))',[$count]);
        $list = self::where('count',$count)->select();
        if ($list) {
            $list = collection($list)->toArray();
        }
        $new_data = [];

        foreach ($months as $v) {
            $n = [];
            $str1 = date('Y-m',$v['create_time']);

            foreach ($list as $m) {
               $str2 = date('Y-m',strtotime($m['create_time']));
                if ($str1 == $str2){
                    array_push($n,$m);
                }
            }
            $new_data[$str1] = $n;

        }

        return $new_data;
    }


    /*
    * 分月的总花费
    * */
    public function findCountBill_month_cost($count){
        $months = self::query('select create_time from ds_daily_bill where count=? group by month(FROM_UNIXTIME(create_time))',[$count]);
        $list = self::where('count',$count)->select();
        if ($list) {
            $list = collection($list)->toArray();
        }
        $new_data = [];

        foreach ($months as $v) {
            $n = [];
            $all=0;
            $str1 = date('Y-m',$v['create_time']);

            foreach ($list as $m) {
                $str2 = date('Y-m',strtotime($m['create_time']));
                if ($str1 == $str2){
                    $all+=$m['cost'];

                }
            }
            $new_data[$str1] = $all;

        }

        return $new_data;
    }


    /*
     * 查询个人每月详细账单
     * */
    public function findMonthBill($para) {
        $count = $para['count'];
        $year_month = $para['time'];
        $page = $para['page'];
        $data = [];

        $month_start = strtotime($year_month);//指定月份月初时间戳
        $month_end = mktime(23, 59, 59, date('m', strtotime($year_month))+1, 00);//指定月份月末时间戳

        $list = self::where('count',$count)->whereTime('create_time','between',[$month_start,$month_end])->order('create_time','desc') -> limit(10*($page-1),10*$page)->field('id,cost,create_time,type,comment,type_id')-> select();
        $data['total'] = self::where('count',$count)->whereTime('create_time','between',[$month_start,$month_end]) -> count();

        if ($list) {
            $list = collection($list)->toArray();
        }
        $data['data'] = $list;

        return $data;
    }

    /**
     * Notes:按照专题账单查询
     * User: 段帅
     * Time: 2018/10/18  下午3:49(摁tab)
     * @param $para
     */
    /*
     * 查询个人每月详细账单
     * */
    public function findTypeBill($para) {
        $count = $para['count'];
        $type_id = $para['type_id'];
        $page = $para['page'];
        $data = [];


        $list = self::where(['count'=>$count,'type_id'=>$type_id])->order('create_time','desc') -> limit(10*($page-1),10*$page)->field('id,cost,create_time,type,comment,type_id')-> select();
        $data['total'] = self::where(['count'=>$count,'type_id'=>$type_id]) -> count();

        if ($list) {
            $list = collection($list)->toArray();
        }
        $data['data'] = $list;

        return $data;
    }






}


